package Engines;

import java.util.*;
import java.io.*;
import java.net.*;
import javax.swing.*;

import DistSys.*;
import Element.*;
import Element.Geom.*;

//import MyGui.*;

public class EngineProcessor{
    public static int ZONEPARAM_ENTIRE_SYSTEM =  0; //old format
    public static int ZONEPARAM_INSIDE        =  1;
    public static int ZONEPARAM_OUTSIDE       =  0;
    /*
     * dZoneToEngine is actually passed to the engine as parameter and is
     * processed in AbsElem.printZoneForEngines().
     * NOTE: It needs to be set before every call of an engine.
     */
    public static double dZoneToEngine = Zone.ENTIRE_SYSTEM;
    public static double dRootCauseZone = Zone.ENTIRE_SYSTEM;
    public static boolean bDisableSag=true;
    public static boolean bDisableCapFlow=true;
    public static boolean bDisable2ndOrder=true;
    public static boolean bUseNetReduction=true;

    public static double[] val = new double[14];
    public static final int TMF=0, TSF=1, TOD=2, TVS=3,TIntKVA=4,TIntKVAHr=5;
    public static final int TKVA=6, TCust=7, TIntCust=8; //total customers, interrupted customers
    public static final int TCapitalCost=9, TOMCost=10;
    public static final int MFCost=11, SFCost=12, VSCost=13;

    public static double[] indices = new double[10];//  maifi=0, saifi=0, saidi=0, caidi=0, asai=0,
                          //caifi=0, asifi=0, asidi=0, ctaidi=0, sarfi=0;
    public static final int MAIFI=0,SAIFI=1,SAIDI=2,CAIDI=3,ASAI=4,
                            CAIFI=5,ASIFI=6,ASIDI=7,CTAIDI=8,SARFI=9;

    public static final String QUICKFLOW_DEBUG  = "QuickFlowControl_Debug.txt";
    public static final String QUICKFLOW_OUT    = "QuickFlowControl_out.txt";
    public static final String QUICKFLOW_IN     = "QuickFlowControl_in.txt";
    public static final String QUICKFLOW_STATUS = "QuickFlowEngineStatus.txt";
    public static final String DISTREL_DEBUG    = "DistrelControl_Debug.txt";
    public static final String DISTREL_OUT      = "DistrelControl_out.txt";
    public static final String DISTREL_IN       = "DistrelControl_in.txt";
    public static final String DISTREL_STATUS   = "DistrelEngineStatus.txt";
    public static final String NETREL_DEBUG     = "NetrelControl_Debug.txt";
    public static final String NETREL_OUT       = "NetrelControl_out.txt";
    public static final String NETREL_IN        = "NetrelControl_in.txt";
    public static final String NETREL_STATUS    = "NetrelEngineStatus.txt";
    public static final String NETFLOW_DEBUG    = "NetFlowControl_Debug.txt";
    public static final String NETFLOW_OUT      = "NetFlowControl_out.txt";
    public static final String NETFLOW_IN       = "NetFlowControl_in.txt";
    public static final String NETFLOW_STATUS   = "NetFlowEngineStatus.txt";
    public static final String RISKRESULTS      = "RiskResults.txt";

    public EngineProcessor() {}

    public static double getZoneAsEngineParamOld() {
        if (dZoneToEngine == Zone.ENTIRE_SYSTEM)
            return ZONEPARAM_ENTIRE_SYSTEM;
        else if (dZoneToEngine == Zone.STUDY_AREA ||
            dZoneToEngine == Zone.SELECTED_AREA)
            return ZONEPARAM_INSIDE;
        else
            return dZoneToEngine;
    }

    public static double getZoneAsEngineParam() {
        return dZoneToEngine;
    }

    private static void preProcessReliabilityIndices(double dZone){
        AbsElem elem;
        double myKW=0, multiplier;

        Vector v;
        if (dZone == Zone.SELECTED_AREA) v = Sys.selectedList;
        else v = Sys.elemList;

        int sz = v.size();
        for (int i = 0; i < val.length; i++) { val[i] = 0.0; }

        for (int i = 0; i < sz; i++) {
            elem = (AbsElem) v.elementAt(i);
            if (!Zone.getZoneFits(dZone, elem)) continue;

            if( elem instanceof Load){
                double cust = elem.getData(AbsElem.DATA_CUSTOMERS);
                double kva = elem.getData(AbsElem.DATA_CONST_POWER);
                val[TMF] += elem.getResult(AbsElem.RES_MOMENT_INT) * cust; //m.f.
                val[TSF] += elem.getResult(AbsElem.RES_SUST_INT) * cust;   //s.f.
                val[TOD] += elem.getResult(AbsElem.RES_OUT_DUR) * cust;    //o.d.
                val[TVS] += elem.getResult(AbsElem.RES_VOLT_SAGS) * cust;//Voltage Sags on Target
                val[TCust] += cust;
                val[TKVA] += kva;
                val[TIntKVA] += elem.getResult(AbsElem.RES_SUST_INT) * kva;
                val[TIntKVAHr] += elem.getResult(AbsElem.RES_OUT_DUR) * kva;
                val[TIntCust] += cust * (1.0-Math.exp(-1.0*elem.getResult(AbsElem.RES_SUST_INT)));

                myKW = cust * elem.getData(AbsElem.DATA_PF_CONST_POWER);
                val[MFCost] += myKW * elem.getResult(AbsElem.RES_MOMENT_INT) * elem.getData(AbsElem.DATA_COST_PER_INT_KW);
                val[SFCost] += myKW * elem.getResult(AbsElem.RES_SUST_INT) * elem.getData(AbsElem.DATA_COST_PER_INT_KW);
                val[SFCost] += myKW * elem.getResult(AbsElem.RES_OUT_DUR) * elem.getData(AbsElem.DATA_COST_PER_INT_KWH);
                val[VSCost] += myKW * elem.getResult(AbsElem.RES_VOLT_SAGS) * elem.getData(AbsElem.DATA_COST_PER_INT_KW);
            }

            if( elem instanceof Line ) { multiplier = elem.getData(AbsElem.DATA_LENGTH); }
            else { multiplier=1.0; }
            val[TCapitalCost] += elem.getData(AbsElem.DATA_CAPITAL_COST) * multiplier;
            val[TOMCost] += elem.getData(AbsElem.DATA_OM_COST) * multiplier;
        }//end of for
    }//end of preProcessReliabilityIndices();

    public static void computeReliabilityIndices(double dZone) {
        preProcessReliabilityIndices(dZone);

        for(int i=0;i<indices.length;i++)
            indices[i]=0.0;
        if(val[TCust]>0.0) {
            indices[MAIFI] = val[TMF] / val[TCust];
            indices[SAIFI] = val[TSF] / val[TCust];
            indices[SAIDI] = val[TOD] / val[TCust];
            indices[ASAI] =(8760.0 - val[TOD] / val[TCust])/8760.0;
            indices[SARFI] = val[TVS] / val[TCust];
        }
        if(val[TSF] > 0.0) {
            indices[CAIDI] = val[TOD] / val[TSF];
        }
        if(val[TIntCust] > 0.0) {
            indices[CAIFI] = val[TSF] / val[TIntCust];
            indices[CTAIDI]= val[TOD] / val[TIntCust];
        }
        if( val[TKVA] > 0.0) {
            indices[ASIFI] = val[TIntKVA] / val[TKVA];
            indices[ASIDI] = val[TIntKVAHr]/val[TKVA];
        }
    }//end of computeReliabilityIndices

    /*==============================================================
      public I/O methods
        1. readDistRelIn()
        2. readRiskResult()
      ==============================================================*/
    public static void writeQuickFlowOut() throws IOException{
        File f = new File(Sys.personalInfo.getTempPath(), QUICKFLOW_OUT);
        f.createNewFile();
        BufferedWriter bw = new BufferedWriter(new FileWriter(f));
        try {
            String s;
            AbsElem e;
            AbsElemGeom g;
            AbsElem parent, parent2, child;

            s = Sys.elemList.size() + "\r\n";
            bw.write(s);
            for(int i=0;i<Sys.elemList.size();i++){
                e = (AbsElem) Sys.elemList.elementAt(i);
                g = e.getElemGeomObj();
                s = e.printOutForQuickFlowEngine(); // output for quick flow engine
                bw.write(s);
            }//end of for
        } catch (IOException e) {
            throw e;
            // JOptionPane.showMessageDialog(null,
            //     "Error Occured @ EngineProcessoar.writeQuickFlowOut()\n Error = "+e );
        } finally {
            if (bw!=null) { bw.close(); }
        }
    }

    public static void writeDistRelOut() throws IOException{
        Sys.computeGroupInfo();

        File f = new File(Sys.personalInfo.getTempPath(), DISTREL_OUT);
        f.createNewFile();
        BufferedWriter bw = new BufferedWriter(new FileWriter(f));
        try {
            AbsElem e;
            AbsElemGeom g;

            String s = Sys.elemList.size() + "\r\n";
            bw.write(s);
            for(int i=0;i<Sys.elemList.size();i++){
                e = (AbsElem) Sys.elemList.elementAt(i);
                g = e.getElemGeomObj();
                s = e.printOutForDistrelEngine();
                bw.write(s);
            }//end of for
        } catch (IOException e) {
            throw e;
            //JOptionPane.showMessageDialog(null,
            //    "Error Occured @ EngineProcessoar.writeDistrelOut()\n Error = "+e );
        } finally {
            if (bw!=null) { bw.close(); }
        }
    }

    public static void writeNetRelOut() throws IOException{
        Sys.computeGroupInfo();

        File f = new File(Sys.personalInfo.getTempPath(), NETREL_OUT);
        f.createNewFile();
        BufferedWriter bw = new BufferedWriter(new FileWriter(f));
        try {
            AbsElem e;
            AbsElemGeom g;

            String s = Sys.elemList.size() + "\r\n";
            bw.write(s);
            for(int i=0;i<Sys.elemList.size();i++){
                e = (AbsElem) Sys.elemList.elementAt(i);
                g = e.getElemGeomObj();
                s = e.printOutForNetrelEngine();
                bw.write(s);
            }//end of for
        } catch (IOException e) {
            throw e;
        } finally {
            if (bw!=null) { bw.close(); }
        }
    }//end of writeNetrelOut();


    //will be the unified format for output to Engines
    public static void writeNetFlowOut() throws IOException{
        Sys.computeGroupInfo();

        File f = new File(Sys.personalInfo.getTempPath(), NETFLOW_OUT);
        f.createNewFile();
        BufferedWriter bw = new BufferedWriter(new FileWriter(f));
        try {
            AbsElem e;
            AbsElemGeom g;

            String s = Sys.elemList.size() + "\r\n";
            bw.write(s);
            for(int i=0;i<Sys.elemList.size();i++){
                e = (AbsElem) Sys.elemList.elementAt(i);
                g = e.getElemGeomObj();
                s = e.printOutForNetFlowEngine();
                bw.write(s);
            }//end of for
        } catch (IOException e) {
            throw e;
        } finally {
            if (bw!=null) { bw.close(); }
        }
    }//end of writeNetrelOut();

    /*=============================================
    read results from Engines into Java GUI
    =============================================*/

    public static void readDistRelIn() throws IOException{
        File f = new File(Sys.personalInfo.getTempPath(), DISTREL_IN);
        BufferedReader br = new BufferedReader(new FileReader(f));

        try {
            String s = br.readLine();
            int state = Integer.parseInt(s);
            if (state != 0) { return; }

            //need to check state next

            AbsElem elem;

            int id;
            for(int i=0;i<Sys.elemList.size();i++){
                s = br.readLine();  //blank line
                s = br.readLine();  //external id
                id = Integer.parseInt(s) - 1; //internal id == external id +1;
                elem = (AbsElem) Sys.elemList.elementAt( id );

                s = br.readLine();
                elem.setResult( AbsElem.RES_MOMENT_INT,Double.parseDouble( s ) );
                s = br.readLine();
                elem.setResult( AbsElem.RES_SUST_INT, Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_OUT_DUR, Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_VOLT_SAGS, Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_SWT_OPER, Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_TRIP_OPER, Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_RECLOSE_OPER, Double.parseDouble( s) );
                s = br.readLine();
                elem.setResult( AbsElem.RES_BLOCK_OPER, Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_CAPA_CONS, Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_CAPA_CONS_KVA, Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_MAIFI,  Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_SAIFI,  Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_SAIDI, Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_SAG,   Double.parseDouble( s ));
            }
        } catch (IOException e) {
            throw e;
            //JOptionPane.showMessageDialog(null,
            //    "Error Occured @ EngineProcessoar.readDistrelIn()\n Error = "+e );
        } finally {
            br.close();
        }
    }//end of readDistrelIn()

    public static void readNetRelIn() throws IOException{
        File f = new File(Sys.personalInfo.getTempPath(), NETREL_IN);
        BufferedReader br = new BufferedReader(new FileReader(f));

        try {
            String s = br.readLine();
            int state = Integer.parseInt(s);
            if( state != 0) { return; } //error occured ?

            AbsElem elem;
            int id;
            for(int i=0;i<Sys.elemList.size();i++){
                s = br.readLine();  //blank line
                s = br.readLine();  //external id
                id = Integer.parseInt(s) - 1; //internal id == external id +1;
                elem = (AbsElem) Sys.elemList.elementAt( id );

                s = br.readLine();
                elem.setResult( AbsElem.RES_MOMENT_INT,Double.parseDouble( s ) );
                s = br.readLine();
                elem.setResult( AbsElem.RES_SUST_INT, Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_OUT_DUR, Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_SWT_OPER, Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_TRIP_OPER, Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_RECLOSE_OPER, Double.parseDouble( s) );
                s = br.readLine();
                elem.setResult( AbsElem.RES_MAIFI,  Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_SAIFI,  Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_SAIDI, Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_CAPA_CONS, Double.parseDouble( s ));
                s = br.readLine();
                elem.setResult( AbsElem.RES_CAPA_CONS_KVA, Double.parseDouble( s) );
            }
        } catch (IOException e) {
            throw e;
        } finally {
            br.close();
        }
    }//end of readNetrelIn()

    public static void readAndExportRiskResults() throws IOException{
        File f = new File(Sys.personalInfo.getTempPath(), RISKRESULTS);
        File f2 = Sys.personalInfo.createTempExportFile();
        BufferedReader br = new BufferedReader(new FileReader(f));
        BufferedWriter bw = new BufferedWriter(new FileWriter(f2));
        try {
            String line = br.readLine();
            int num = Integer.parseInt(line);

            bw.write("Performance Advantage Risk Analysis\n");
            bw.write("# of Samples: " + num + "\n\n");
            bw.write("Year,MAIFI,SAIFI,SAIDI\n");

            String str;
            for(int i=0;i<num;i++){
                line = br.readLine();
                bw.write(""+(i+1)+",");
                bw.write(line); //line does not contain "\n"
                bw.write("\n");
            }
        } catch (IOException e) {
            throw e;
            //JOptionPane.showMessageDialog(null,
            //    "Error Occured @ EngineProcessoar.readRiskResult()\n Error = "+e );
        } finally {
            bw.close();
            br.close();
        }
        Sys.personalInfo.execExcel(f2.getPath());
    }

    public static double[][] readRiskResults() throws IOException{
        double[][] rtn;
        File f = new File(Sys.personalInfo.getTempPath(), RISKRESULTS);
        BufferedReader br = new BufferedReader(new FileReader(f));
        try {
            String line = br.readLine();
            int num = Integer.parseInt(line);
            rtn = new double[3][];
            rtn[0] = new double[num];
            rtn[1] = new double[num];
            rtn[2] = new double[num];

            String str;
            StringTokenizer strToken;
            for(int i=0;i<num;i++){
                str = br.readLine();
                strToken = new StringTokenizer(str, ",");
                rtn[0][i] = Double.parseDouble( strToken.nextToken() );
                rtn[1][i] = Double.parseDouble( strToken.nextToken() );
                rtn[2][i] = Double.parseDouble( strToken.nextToken() );
            }
        } catch (IOException e) {
            throw e;
        } finally {
            br.close();
        }
        return rtn;
    }//end of readRiskResult2();

    public static void readQuickFlowIn() throws Exception{
        File f = new File(Sys.personalInfo.getTempPath(), QUICKFLOW_IN);
        BufferedReader br = new BufferedReader(new FileReader(f));
        try {
            String s = br.readLine();
            int state = Integer.parseInt(s);
            if (state != 0) return;

            AbsElem elem;
            AbsElemGeom geom;
            double val;

            int id;
            for(int i=0; i<Sys.elemList.size(); i++){
                s = br.readLine();  //blank line
                s = br.readLine();  //external id
                id = Integer.parseInt(s)-1; //internal id = external id -1;
                elem = (AbsElem) Sys.elemList.get( id );
                geom = elem.getElemGeomObj();

                // unbalanced phase multipliers
                double im = 0.0;
                im = geom.ubiMult[elem.getPhase()];

                for (int j=2;j<11;j++){
                    s = br.readLine();
                    if (s.equalsIgnoreCase("-1.#IND")) { val=0.0; }//!!! hard code; need to be flexible
                    else { val= Double.parseDouble( s ); }

                    if (j==2)      elem.setResult( AbsElem.RES_VOLT_MAG, val);
                    else if (j==3) elem.setResult( AbsElem.RES_VOLT_ANG, val);
                    else if (j==4) elem.setResult( AbsElem.RES_CURR_MAG, (val * im)); // phase multiplier 'im'
                    else if (j==5) elem.setResult( AbsElem.RES_CURR_ANG, val);
                    else if (j==6) elem.setResult( AbsElem.RES_PF, val);
                    else if (j==7) elem.setResult( AbsElem.RES_VOLT_DROP, val);
                    else if (j==8) elem.setResult( AbsElem.RES_TAP_POS, val);
                    else if (j==9) elem.setResult( AbsElem.RES_CURR_LOADING, val);
                    else if (j==10)elem.setResult( AbsElem.RES_KVA_FLOW, val);

                    double kvaRating = 0.0;
                    if( elem.getData( AbsElem.DATA_RATED_POWER ) != 0.0 ){
                        kvaRating = 100.0 * elem.getResult(AbsElem.RES_KVA_FLOW)
                            / elem.getData( AbsElem.DATA_RATED_POWER ) ;
                    }
                    elem.setResult(AbsElem.RES_KVA_RATING, kvaRating);
                }
              }//end of for loop
        } catch (Exception e) {
            throw e;
            //JOptionPane.showMessageDialog(null, "Error Occured @ EngineProcessoar.readQuickFlowIn\n error = "+e );
        } finally {
           br.close();
        }
    }//end of readQuickFlowIn()

    public static void readNetFlowIn() throws Exception{
        File f = new File(Sys.personalInfo.getTempPath(), NETFLOW_IN);
        BufferedReader br = new BufferedReader(new FileReader(f));
        try {
            String s = br.readLine();
            int state = Integer.parseInt(s);
            if (state != 0) return;

            AbsElem elem;
            AbsElemGeom geom;
            double val;

            int id;
            for(int i=0;i<Sys.elemList.size();i++){
                s = br.readLine();  //blank line
                s = br.readLine();  //external id
                id = Integer.parseInt(s)-1; //internal id = external id -1;
                elem = (AbsElem) Sys.elemList.get( id );
                geom = elem.getElemGeomObj();

                // unbalanced phase multipliers
                double im = 0.0;
                im = geom.ubiMult[elem.getPhase()];

                for (int j=2;j<=13;j++){
                    s = br.readLine();
                    if (s.equalsIgnoreCase("-1.#IND")) { val=0.0; }//!!! hard code; need to be flexible
                    else { val= Double.parseDouble( s ); }

                    if      (j==2) elem.setResult( AbsElem.RES_VOLT_MAG_NODE1, val);
                    else if (j==3) elem.setResult( AbsElem.RES_VOLT_ANG_NODE1, val);
                    else if (j==4) elem.setResult( AbsElem.RES_VOLT_MAG, val);
                    else if (j==5) elem.setResult( AbsElem.RES_VOLT_ANG, val);
                    else if (j==6) elem.setResult( AbsElem.RES_CURR_MAG, (val * im)); // phase multiplier 'im'
                    else if (j==7) elem.setResult( AbsElem.RES_CURR_ANG, val);
                    else if (j==8) elem.setResult( AbsElem.RES_PF, val);
                    else if (j==9) elem.setResult( AbsElem.RES_VOLT_DROP, val);
                    else if (j==10) elem.setResult( AbsElem.RES_TAP_POS, val);
                    else if (j==11) elem.setResult( AbsElem.RES_CURR_LOADING, val);
                    else if (j==12) elem.setResult( AbsElem.RES_KVA_FLOW, val);
                    else if (j==13) elem.setResult( AbsElem.RES_REVERSED_FLOW, val);

                    double kvaRating = 0.0;
                    if( elem.getData( AbsElem.DATA_RATED_POWER ) != 0.0 ){
                        kvaRating = 100.0 * elem.getResult(AbsElem.RES_KVA_FLOW)
                            / elem.getData( AbsElem.DATA_RATED_POWER ) ;
                    }
                    elem.setResult(AbsElem.RES_KVA_RATING, kvaRating);
                }
              }//end of for loop
        } catch (Exception e) {
            throw e;
            //JOptionPane.showMessageDialog(null, "Error Occured @ EngineProcessoar.readQuickFlowIn\n error = "+e );
        } finally {
           br.close();
        }
    }//end of readNetFlowIn()


    /**
     * Short circuit uses the same activeX control engine as NetFlow.
     * The result file is also the same as Netflow, NETFLOW_IN.
     * But the format is different.
     */
    public static void readShortCircuitIn() throws Exception{
        File f = new File(Sys.personalInfo.getTempPath(), NETFLOW_IN);
        BufferedReader br = new BufferedReader(new FileReader(f));
        try {
            String s = br.readLine();
            int state = Integer.parseInt(s);
            if (state != 0) return;

            AbsElem elem;
            AbsElemGeom geom;
            double val;

            int id;
            for(int i=0;i<Sys.elemList.size();i++){
                s = br.readLine();  //blank line
                s = br.readLine();  //external id
                id = Integer.parseInt(s)-1; //internal id = external id -1;
                elem = (AbsElem) Sys.elemList.get( id );
                geom = elem.getElemGeomObj();

                // unbalanced phase multipliers
                double im = 0.0;
                im = geom.ubiMult[elem.getPhase()];

                for (int j=0; j<3; j++){
                    s = br.readLine();
                    if (s.equalsIgnoreCase("-1.#IND")) { val=0.0; }
                    else { val= Double.parseDouble( s ); }
                    if      (j==0) { elem.setResult( AbsElem.RES_SHORTCIRC_CURR, val/1000.0);}//in kA
                    else if (j==1) { elem.setResult( AbsElem.RES_VOLT_SAGS_NODE1, val); }
                    else if (j==2) { elem.setResult( AbsElem.RES_VOLT_SAGS, val); }
                }

                //total 11 entries; bin size=0.1; [0,1] +(1, INFINITE)
                for (int j=0; j<2* AbsElem.NUM_SAG_FROM_SHTCKT; j++){
                    s = br.readLine();
                    if (s.equalsIgnoreCase("-1.#IND")) { val=0.0; }//!!! hard code; need to be flexible
                    else { val= Double.parseDouble( s ); }
                    if( j < AbsElem.NUM_SAG_FROM_SHTCKT) {
                        elem.adSagNode1[j] = val; }
                    else {
                        elem.adSagNode2[j - AbsElem.NUM_SAG_FROM_SHTCKT ] = val; }
                }

                s = br.readLine();
                val = Integer.parseInt( s );
                elem.setResult( AbsElem.RES_REVERSED_FLOW, val);
            }//end of for loop
        } catch (Exception e) {
            throw e;
        } finally {
           br.close();
        }
    }//end of readShortCircuitIn()

    /**
     * Security assessment uses the same activeX control engine as NetFlow.
     * The result file is also the same as Netflow, NETFLOW_IN.
     * But the format is different.
     */
    public static void readSecurityIn() throws Exception{
        File f = new File(Sys.personalInfo.getTempPath(), NETFLOW_IN);
        BufferedReader br = new BufferedReader(new FileReader(f));
        try {
            String s = br.readLine();
            int state = Integer.parseInt(s);
            if (state != 0) return;

            AbsElem elem;
            AbsElemGeom geom;
            double val;

            int id;
            for(int i=0;i<Sys.elemList.size();i++){
                s = br.readLine();  //blank line
                s = br.readLine();  //external id
                id = Integer.parseInt(s)-1; //internal id = external id -1;
                elem = (AbsElem) Sys.elemList.get( id );

                s = br.readLine();
                elem.setResult( AbsElem.RES_PROTECT_ZONE, Double.parseDouble(s));

                s = br.readLine();
                elem.setResult( AbsElem.RES_SA_MOMENT_INT, Double.parseDouble(s));
                s = br.readLine();
                elem.setResult( AbsElem.RES_SA_SUST_INT, Double.parseDouble(s));
                s = br.readLine();
                elem.setResult( AbsElem.RES_SA_DUR_SI, Double.parseDouble(s));

                s = br.readLine();
                elem.setResult( AbsElem.RES_OLV_MI, Double.parseDouble(s));
                s = br.readLine();
                elem.setResult( AbsElem.RES_OLV_SI, Double.parseDouble(s));
                s = br.readLine();
                elem.setResult( AbsElem.RES_OLV_DUR_SI, Double.parseDouble(s));

                s = br.readLine();
                elem.setResult( AbsElem.RES_UVV_MI, Double.parseDouble(s));
                s = br.readLine();
                elem.setResult( AbsElem.RES_UVV_SI, Double.parseDouble(s));
                s = br.readLine();
                elem.setResult( AbsElem.RES_UVV_DUR_SI, Double.parseDouble(s));

            }//end of for loop
        } catch (Exception e) {
            throw e;
        } finally {
           br.close();
        }
    }//end of readSecurityIn()
}